package site.marqstree.kotlin.usercenter.ui.activity.resetpwd

import android.os.Bundle
import kotlinx.android.synthetic.main.activity_reset_pwd.*
import org.jetbrains.anko.clearTop
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.singleTop
import org.jetbrains.anko.toast
import site.marqstree.kotlin.library.base.extent.enable
import site.marqstree.kotlin.library.base.extent.onClick
import site.marqstree.kotlin.library.base.ui.activity.BaseMvpActivity
import site.marqstree.kotlin.usercenter.R
import site.marqstree.kotlin.usercenter.injection.component.DaggerUserComponent
import site.marqstree.kotlin.usercenter.ui.activity.login.LoginActivity


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.usercenter.ui.activity
 * 文件名: ResetPwdActivity
 * 创建者: marqstree
 * 创建时间: 2020/2/22 16:12
 * 描述: TODO
 */

/*
    重置密码界面
 */
class ResetPwdActivity : BaseMvpActivity<ResetPwdPresenter>(),
    ResetPwdView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reset_pwd)

        initView()

    }

    /*
        初始化视图
     */
    private fun initView() {

        mConfirmBtn.enable(mPwdEt,{isBtnEnable()})
        mConfirmBtn.enable(mPwdConfirmEt,{isBtnEnable()})

        mConfirmBtn.onClick {
            if (mPwdEt.text.toString() != mPwdConfirmEt.text.toString()){
                toast("密码不一致")
                return@onClick
            }
            mPresenter.resetPwd(intent.getStringExtra("mobile"),mPwdEt.text.toString())
        }
    }

    override fun initBusinessInjection() {
        DaggerUserComponent
            .builder()
            .activityComponent(activityComponent)
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    /*
        判断按钮是否可用
     */
    private fun isBtnEnable():Boolean{
        return mPwdEt.text.isNullOrEmpty().not() &&
                mPwdConfirmEt.text.isNullOrEmpty().not()
    }

    /*
        重置密码回调
     */
    override fun onResetPwdResult(result: String) {
        toast(result)
        //清空栈，并以单例栈顶模式启动登陆Activity
        //singleTop:栈顶单实例
        //clearTop:销毁目标Activity和它之上的所有Activity，重新创建目标Activity
        startActivity(intentFor<LoginActivity>().singleTop().clearTop())
        finish()
    }
}